<?php

namespace app\api\controller;

use think\Controller;
use think\Cache;

class Wallet extends Common
{

	//我的钱包列表
	public function index()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		
		$data = $this->get_wallet_user();
		
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
	}

	// 行情提取
	public function price($type)
	{		
		foreach(moneyprice() as $k => $v)
		{	
			if($v['type'] == $type){
				return $v['today_price'];
			}
		}
		
	}

	// 获取私钥
	public function privatekey()
	{		
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		input('secpwd')? input('secpwd') : apifasong(['zt' => -1, 'msg' => '缺少参数']);
		if ($userinfo['secpwd'] <> MD5($appdata['secpwd'])) apifasong(['zt' => -1, 'msg' => "支付密码错误"]);

		$privatekey = db('wallet')->where(array('uid' => $userinfo['id']))->value('privatekey');

		if (!$privatekey) {
			$privatekey = '';
		}
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $privatekey]);
	}
	

	//二维码
	public function code()
	{	

		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$address = wallet_sql($userinfo['id'],'address');
		$contract = contract();
		$data = array();
		$code = array();
		foreach($contract as &$v){
			$code['type'] = $v['type'];
			$code['to'] = $address;
			// $v['code'] = $this->getcode($contract);
			$v['img'] = 'http://'.$_SERVER['SERVER_NAME'].'/'.getcode(json_encode($code));
			unset($v['unit']);
		}

		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $contract]);

	} 

	// 我的钱包->交易记录
	public function record()
	{	
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		$page = input('page') ? input('page') : 0;
		$address  = wallet_sql($userinfo['id'],'address');
		$contract = contract();

		$data = array();
		$i = 0;
		foreach($contract as $k => $v){
			$params['page'] = $page;
			$params['offset'] = 10;
			$params['address'] = $address;
			$params['contractaddress'] = $v['contractaddress'];
			
			$wallet_record = wallet_record($v['type'],$params);
			if (isset($wallet_record['code'])) {
				foreach($wallet_record['data'] as $kk => $vv) {
					if($vv['value'] != '0'){
						$data[$i]['time'] = $vv['timeStamp'];
						$data[$i]['address'] = $vv['from'];
						if ($vv['from'] == $address) {
							$data[$i]['money'] = '-'.(bcdiv($vv['value'], pow(10, $v['unit']), 4) * 1).' '.$v['type'];
						}else{
							$data[$i]['money'] = '+'.(bcdiv($vv['value'], pow(10, $v['unit']), 4) * 1).' '.$v['type'];
						}
						$i++;
					}
				}
			}
		}

		if ($data) {
			foreach($data as &$v){
				$v['time'] = date('Y-m-d H:i:s', $v['time']);
				$v['address'] = substr($v['address'],0,18).'...';
			}
		}

		array_multisort(array_column($data, 'time'), SORT_DESC, $data);
		
		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);

	}

	// 我的转出页面加载
	public function rollout()
	{	
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;
		// $address = wallet_sql($userinfo['id'],'address');

		$wallet_index =  $this->get_wallet_user();
		$data = array();
		foreach($wallet_index['wallet'] as $k => $v){
			$data[$k]['type'] = $v['type'];
			$data[$k]['money'] = $v['available'];
			$data[$k]['rmbrate'] = rmbrate();
			$data[$k]['poundage'] = eth_gasPrice($v['type']) .' ETH';
		}

		apifasong(['zt' => 1, 'msg' => '操作成功', 'data' => $data]);
	}

	// 转账
	public function transfer()
	{
		$appdata = $this->appdata;
		$userinfo = $this->userinfo;

		if (!input('money')) {
			apifasong(['zt' => -1, 'msg' => '请输入金额']);
		}

		if (!input('to')) {
			apifasong(['zt' => -1, 'msg' => '请输入地址']);
		}elseif(strlen(input('to')) != 42){
			apifasong(['zt' => -1, 'msg' => '请输入正确的地址']);
		}

		$params = array();
		$type = input('type');
		$contractaddress = '';
		$contract = contract();
		$privatekey = wallet_sql($userinfo['id'],'privatekey');
		$address = wallet_sql($userinfo['id'],'address');

		foreach($contract as $v) {
			if ($v['type'] == strtoupper($type)) {
				$params['contractaddress'] = $v['contractaddress'];
				$params['unit'] = $v['unit'];
			}
		}
		$params['privatekey'] = $privatekey;
		$params['value'] = input('money');
		$params['to'] = input('to');
		$return = transfer($type,$params);
		if (!$return['code']) {
			apifasong(['zt' => -1, 'msg' => $return['msg']]);
		}
		if (!$return) {
			apifasong(['zt' => -1, 'msg' => '转账失败']);
		}
		apifasong(['zt' => 1, 'msg' => '交易处理中，等待到账']);

	}

	// 加载个人用户钱包信息
	private function get_wallet_user()
	{	
		$userinfo = $this->userinfo;

		if (Cache::get('wallet_index'.$userinfo['id'])) {
			$data = Cache::get('wallet_index'.$userinfo['id']);
		}else{
			$data = array(); 
			$data['count'] = 0; 
			$data['wallet'] = array(); 
			$address = wallet_sql($userinfo['id'],'address');
			$contract = contract(); // 合约地址
			$i = 0;
			foreach($contract as $k => $v){
				$params['contractaddress'] = $v['contractaddress'];
				$params['address'] = $address;
				$params['unit'] = $v['unit'];

				$today_price = $this->price($v['type']);
				$getbalance = getbalance($v['type'],$params);
				$cny = round($getbalance * $today_price,2);
				$data['wallet'][$i]['type'] = $v['type']; // 类型
				$data['wallet'][$i]['available'] = round($getbalance,2);  // 货币余额
				$data['wallet'][$i]['cny'] = $cny * 1;   //货币汇率
				$data['wallet'][$i]['img'] = $v['img'];
				$data['count'] += $cny; // 货币累计汇率
				$i++;
			}
			Cache::set('wallet_index'.$userinfo['id'], $data, 180);
		}

		return $data;
	}
	
}
